package com.homwee.factory.util.view;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;

public class FactoryMacWriteView extends TextView implements OnFocusChangeListener{
	public boolean flag=false;
	private int count = 0;
	private Timer timer;
	private TimerTask task;
	private SpannableString mvalue;
	private String[] mArrayValue={"0","1","2","3","4","5","6","7","8","9"};
	private String[] mGhArrayValue={"a","b","c","d","e","f"};
	private int  moption=-1;
	private  static int  location =0;
	private int  mGhoption=-1;
	private String showvalue="a";
	private static String macString = "";

	private static final long delay = 10;
	private static final long period = 500;
	
	public FactoryMacWriteView(Context context, AttributeSet attrs){
		super(context, attrs);
		this.setFocusable(true);
		this.setOnFocusChangeListener(this);
		this.setTextColor(Color.YELLOW);
	}
	
	public void onUp() {
		if(flag){
			 ++ moption;
			 moption= moption%(mArrayValue.length);
			 showvalue=mArrayValue[moption];
			 mvalue=new SpannableString(showvalue);
			 Log.v("--onUp--","moption=="+moption); 
			 }
		}
	
	public void onDown() {
		if(flag){
			 ++ mGhoption;
			 mGhoption= mGhoption%(mGhArrayValue.length);
			 showvalue=mGhArrayValue[mGhoption];
			 mvalue=new SpannableString(showvalue);
			 Log.v("--onDown--","mGhoption=="+mGhoption);	 
			 }
		}
	public int getOptionLocation() {
		return   location ;	
	}
	
	public void setShowValue(String value) {
		showvalue  =  value ;	
	}
	public void onkey0() {
		showvalue="0";
	}
	
	public void onkey1() {
		showvalue="1";
	}
	public void onkey2() {
		showvalue="2";
	}
	public void onkey3() {
		showvalue="3";
	}
	public void onkey4() {
		showvalue="4";
	}
	public void onkey5() {
		showvalue="5";
	}
	public void onkey6() {
		showvalue="6";
	}
	public void onkey7() {
		showvalue="7";
	}
	public void onkey8() {
		showvalue="8";
	}
	public void onkey9() {
		showvalue="9";
	}
		
	public void draw() {	
		timer = new Timer("WriteMac");
		task = new TimerTask() {
			public void run() {
					if(count%2==0){
						mvalue=null;
						mvalue=new SpannableString(showvalue+"|");
						mvalue.setSpan(new ForegroundColorSpan(Color.RED), 1, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						mvalue.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, 1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						
					}else{
						mvalue=null;
						mvalue=new SpannableString(showvalue+"|");
						mvalue.setSpan(new ForegroundColorSpan(Color.WHITE), 1, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						mvalue.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, 1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						
					}
					FactoryMacWriteView.this.post(new Runnable() {
					public void run() {
						setText("");
						setText(mvalue);
					}
				});
				count++;
			}
		};
		timer.schedule(task, delay, period);
	}
	// stop drawing
	public void stopDraw() {
		task.cancel();
		timer.cancel();
		location++;
		if (location==12){
			location=0;
		}
		macString+=showvalue;
		setText(showvalue);
		
	}
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}
	public void setValue(String mValue){		
		showvalue=mValue;
		setText(showvalue);
	}
	public void onFocusChange(View v, boolean hasFocus){
		if(hasFocus){
			flag=true;
			SpannableString mStartvalue  = new SpannableString(showvalue+"|");;
			setText(showvalue+"|");
			mStartvalue.setSpan(new ForegroundColorSpan(Color.WHITE), 1, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			mStartvalue.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, 1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			setText(mStartvalue);
			draw();		
		}
		else{
			flag=false;
			stopDraw();		
		}
		
	}
}
